package com.guigu.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.mapper.RolesMapper;
import com.guigu.mapper.SystemuUserRoleMapper;
import com.guigu.pojo.Roles;
import com.guigu.pojo.SystemuUserRole;
import com.guigu.service.RolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class RolesServiceImpl extends ServiceImpl<RolesMapper, Roles> implements RolesService {

    @Autowired
    RolesMapper rolesMapper;

    @Autowired
    SystemuUserRoleMapper systemuUserRoleMapper;

    //分页显示所有角色
    @Override
    public Page<Roles> showallroles(Integer pageno, Integer pagesize, Roles roles) {

        QueryWrapper<Roles> queryWrapper
                = new QueryWrapper<Roles>();

        if (!StringUtils.isEmpty(roles.getRoleName())){
            queryWrapper.like("role_name",roles.getRoleName());
        }

        Page<Roles> page = new Page<Roles>(pageno,pagesize);
        page.setPages(pageno);
        page.setSize(pagesize);

        return rolesMapper.selectPage(page,queryWrapper);
    }

    //修改状态
    @Override
    public Map eidebystate(Roles roles) {
        Map map = new HashMap();
        map.put("msg","失败");
        QueryWrapper<SystemuUserRole> queryWrapper
                =new QueryWrapper<SystemuUserRole>();


        queryWrapper.eq("r_id",roles.getId());

        if (systemuUserRoleMapper.selectCount(queryWrapper)>0){
            map.put("msg","该角色已被使用不能冻结");
        }else {
            if (roles.getRoleState()==0){
                roles.setRoleState(1);
                rolesMapper.updateById(roles);
                map.put("msg","成功");
            }else {
                roles.setRoleState(0);
                rolesMapper.updateById(roles);
                map.put("msg","成功");
            }
        }
        return map;
    }

    //添加
    @Override
    public Map addroles(Roles roles) {
        Map map = new HashMap();
        map.put("msg","添加失败");
        roles.setRoleState(0);
        if (rolesMapper.insert(roles)>0){
            map.put("msg","添加成功");
        }

        return map;
    }

    //根据id查找角色
    @Override
    public Roles querybyid_roles(Roles roles) {
        return rolesMapper.selectById(roles.getId());
    }

    //修改角色
    @Override
    public Map eiderole(Roles roles) {
        Map map = new HashMap();

        map.put("msg","修改失败");

        if (rolesMapper.updateById(roles)>0){
            map.put("msg","修改成功");
        }

        return map;
    }
}
